package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.domain.Tag;
import com.smm.web.vmc.domain.Warehouse;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.service.TagService;
import com.smm.web.vmc.service.WarehouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/7.
 *
 */
@Controller
@RequestMapping("/admin/tag")
public class TagController {

    @Resource
    private TagService tagService;

    @Resource
    private WarehouseService warehouseService;

    @RequestMapping("/listByWh.do")
    public ModelAndView listByWh(int whId){

        ModelAndView mv = new ModelAndView("admin/tag/list");

        //获取指定仓库下所有标签列表
        List<Tag> tagList = tagService.getAllByWarehouse(whId);

        //获取仓库信息
        Warehouse warehouse = warehouseService.getOneById(whId);

        mv.addObject("tagList",tagList);
        mv.addObject("warehouse",warehouse);


        return mv;
    }


    @RequestMapping("/del.do")
    @ResponseBody
    public JsonReturn del(String no){

        try {

            tagService.delete(no);

            return new JsonReturn(1,"删除成功");

        } catch (Exception e) {

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());
        }

    }


    @RequestMapping("/add.do")
    @ResponseBody
    public JsonReturn add(String no,int whId){

        try {

            if(StringUtils.isBlank(no)) throw new Exception("RFID编号不能为空");

            tagService.add(no,whId);

        } catch (Exception e) {
            e.printStackTrace();

            return new JsonReturn(0,"新增失败！"+e.getMessage());

        }

        return new JsonReturn(1,"新增成功");

    }






}
